package wafa.jmsListener;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import wafa.exceptions.AFamilyNotFound;
import wafa.gedInterface.GedService;
import wafa.modelDocument.Document;

public class JmsListener implements MessageListener  {
   GedService gedservice;
	
   private JmsTemplate myjmsTemplate;
	
	@Override
	public void onMessage(Message message){
		// TODO Auto-generated method stub
	
		
		
		try {
			
			
			System.out.println("correlation ID:"+message.getJMSCorrelationID());
			
			Logger monLog = Logger.getLogger(JmsListener.class.getName());
			FileHandler logfile=new FileHandler();
			logfile.setFormatter(new SimpleFormatter());
			monLog.setLevel(Level.ALL);
			monLog.addHandler(logfile);
		
		
			monLog.log(Level.INFO," debut reception message");
			
			System.out.println("debut reception message");
			ObjectMessage msg = (ObjectMessage) message;
			final Document document= (Document) msg.getObject();
			
			
			
			System.out.println("le nom de la  famille est:"+document.getFamily().getFamilyName());			
			
				
					
						try {
							gedservice.addDocument(document);
						} catch (AFamilyNotFound e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
			

			System.out.println("envoi ackit!!!!!!!!!!!!");
			
			
			System.out.println("000000000000....."+myjmsTemplate);
			
			//***********************
			myjmsTemplate.send(new MessageCreator() {
				public Message createMessage(final Session session)
						throws JMSException {
					return session.createTextMessage("fffffffffffff");
				}
			});	
		
			
			
			
			
			
			
			
			System.out.println("fin reception message");
			
			
			
			} catch (JMSException e) {
				 System.out.println("22222222222222");
			e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				 System.out.println("33333333333333333");
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				 System.out.println("4444444444444444444444");
				e.printStackTrace();
			} 
			
			
			
			
	}



	public GedService getGedservice() {
		return gedservice;
	}



	public void setGedservice(GedService gedservice) {
		this.gedservice = gedservice;
	}



	public JmsTemplate getMyjmsTemplate() {
		return myjmsTemplate;
	}



	public void setMyjmsTemplate(JmsTemplate myjmsTemplate) {
		this.myjmsTemplate = myjmsTemplate;
	}
	
	
	

}
